let getBLValues=function(values,dir,start,end){
        let data=[];
        values.forEach(e=>{
            if(dir==='top-bottom'){
                if(e.r===start) data.push(e.value)
            }else{
                if(e.c===start) data.push(e.value)
            }
        });
        return data
    }

let getValues=function(values,dir,loc){
        let data=[];
        values.forEach(e=>{
            if(dir==='top-bottom'){
                if(e.c===loc) data.push(e.value)
            }else if(dir==='left-right'){
                if(e.r===loc) data.push(e.value)
            }else{
                data.push(e.value);
            }
        });
        return data
    }

class Parser {
    constructor(){}
    static options(){
        return {
            line:{
                style:[],
                base:['legend','category','value']
            }
            ,bar:{
                style:[],
                base:['legend','category','value']
            }
            ,pie:{
                style:[],
                base:['legend','category','value']
            }
            ,map:{
                style:[],
                base:['legend','category','value','long','lat','maptype']
            }
            ,scatter:{
                style:[],
                base:['legend','category','value','yvalue']
            }
            ,radar:{
                style:[],
                base:['legend','category','value','maxvalue']
            }
            ,funnel:{
                style:[],
                base:['legend','category','value']
            }
            ,gauge:{
                style:[],
                base:['legend','category','value','minvalue','maxvalue']
            }
        };
    }
    static bar(data){
        if(!data.legend||!data.category||!data.value) return null;
        //检查通过，开始生成图表
        let cdata={
            legend:data.legend.value.map(e=>{return e.value}),
            category:data.category.value.map(e=>{return e.value}),
            series:[]
        };
        let legendarr=[],legend=data.legend.value,dir=data.legend.direction;
        for(let i=0,l=legend.length;i<l;i++){
            let s,e;
            if(dir==='top-bottom'){
                s=legend[i].r;
                e=i<l-1?legend[i+1].r:-1;
            }else{
                s=legend[i].c;
                e=i<l-1?legend[i+1].c:-1;
            }
            legendarr.push({s:s,e:e,value:legend[i].value,data:[]});
        }
        cdata.series=legendarr.map(e=>{
            return {
                name:e.value,
                type:'bar',
                stack:e.value,
                data:getBLValues(data.value.value,dir,e.s,e.e)
            }
        });

        let option = {
            title: {
                text: ''
            },
            tooltip : {
                trigger: 'axis'
            },
            legend: {
                data:cdata.legend,
                x: 'left'
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            xAxis : [
                {
                    type : 'category',
                    data : cdata.category
                }
            ],
            yAxis : [
                {
                    type : 'value'
                }
            ],
            series : cdata.series
        };
        return option
    }

    static line(data){
        if(!data.legend||!data.category||!data.value) return null;
        //检查通过，开始生成图表
        let cdata={
            legend:data.legend.value.map(e=>{return e.value}),
            category:data.category.value.map(e=>{return e.value}),
            series:[]
        };
        let legendarr=[],legend=data.legend.value,dir=data.legend.direction;
        for(let i=0,l=legend.length;i<l;i++){
            let s,e;
            if(dir==='top-bottom'){
                s=legend[i].r;
                e=i<l-1?legend[i+1].r:-1;
            }else{
                s=legend[i].c;
                e=i<l-1?legend[i+1].c:-1;
            }
            legendarr.push({s:s,e:e,value:legend[i].value,data:[]});
        }
        cdata.series=legendarr.map(e=>{
            return {
                name:e.value,
                type:'line',
                stack:e.value,
                data:getBLValues(data.value.value,dir,e.s,e.e)
            }
        });

        let option = {
            title: {
                text: ''
            },
            tooltip : {
                trigger: 'axis'
            },
            legend: {
                data:cdata.legend,
                x: 'left'
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            xAxis : [
                {
                    type : 'category',
                    data : cdata.category
                }
            ],
            yAxis : [
                {
                    type : 'value'
                }
            ],
            series : cdata.series
        };
        return option
    }

    static funnel(data){
        if(!data.legend||!data.category||!data.value) return null;
        //检查通过，开始生成图表
        let legends=data.legend.value;
        let cdata={
            legend:[legends[0].value],
            category:data.category.value.map(e=>{return e.value}),
            series:[]
        };
        let dir=data.category.direction,legend=data.legend.value[0];
        let loc=dir==='top-bottom'?legend.c:legend.r;
        let values=getValues(data.value.value,dir,loc);
        cdata.series=[{
            name:cdata.legend,
            type:'funnel',
            sort: 'descending',
            gap: 2,
            width: '80%',
            label: {
                normal: {
                    show: true,
                    position: 'inside'
                },
                emphasis: {
                    textStyle: {
                        fontSize: 20
                    }
                }
            },
            data: cdata.category.map((e,i)=>{
                return {name:e,value:values[i]}
            })
        }];
        let option = {
            tooltip: {
                trigger: "item",
                formatter: "{a} <br/>{b} : {c}"
            },
            legend: {
                // orient: 'vertical',
                // x: 'left',
                data:cdata.legend
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            series : cdata.series
        };
        return option;
    }

    static gauge(data){
        if(!data.legend||!data.category||!data.value||!data.tvalue) return null;
        // 检查通过，开始生成图表
        let legends=data.legend.value;
        let cdata={
            legend:[legends[0].value],
            category:data.category.value[0].value,
            min:data.minvalue.value[0].value,
            max:data.maxvalue.value[0].value,
            series:[]
        };
        let dir=data.category.direction,legend=data.legend.value[0];
        let loc=dir==='top-bottom'?legend.c:legend.r;
        let values=getValues(data.value.value,dir,loc);
        cdata.series=[{
            name:cdata.legend,
            type:'gauge',
            detail: {formatter:'{value}%'},
            data: [{name:cdata.category, value:values[0].toFixed(1)}]
        }];
        let option = {
            tooltip: {
                trigger: "item",
                formatter:function(obj){
                    return obj.data.name+' : '+obj.data.v
                }
            },
            legend: {
                data:cdata.legend
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            series : cdata.series
        };
        return option;
    }
    static map(data,maptype){
        if(!data.legend||!data.category||!data.value||!data.long||!data.lat) return null;
        //检查通过，开始生成图表
        let legends=data.legend.value;
        let cdata={
            maptype:maptype||'china',
            legend:[legends[0].value],
            category:data.category.value.map(e=>{return e.value}),
            series:[]
        };
        let dir=data.category.direction,legend=data.legend.value[0];
        let loc=dir==='top-bottom'?legend.c:legend.r;
        let values=getValues(data.value.value,dir,loc);
        let longs=data.long.value.map(e=>{return e.value});
        let lats=data.lat.value.map(e=>{return e.value});
        cdata.series=[{
            backgroundColor: '#404a59',
            name:cdata.legend,
            type:'scatter',
            coordinateSystem: 'geo',
            symbolSize: function (val) {
                return val[2] / 10;
            },
            legend: {
                orient: 'vertical',
                y: 'bottom',
                x:'right',
                data:cdata.legend,
                textStyle: {
                    color: '#fff'
                }
            },
            label: {
                normal: {
                    formatter: '{b}',
                    position: 'right',
                    show: false
                },
                emphasis: {
                    show: true
                }
            },
            itemStyle: {
                normal: {
                    color: '#ddb926'
                }
            },
            data:cdata.category.map((e,i)=>{
                return {name:e,value:[longs[i],lats[i],values[i]]}
            })
        }];
        let option = {
            tooltip: {
                trigger: "item",
                formatter: function(val){
                    return val.data.name+" : "+val.data.value[2];
                }
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            geo: {
                map: cdata.maptype,
                label: {
                    emphasis: {
                        show: false
                    }
                },
                roam: true,
                itemStyle: {
                    normal: {
                        areaColor: '#323c48',
                        borderColor: '#111'
                    },
                    emphasis: {
                        areaColor: '#2a333d'
                    }
                }
            },
            series : cdata.series
        };
        return option;
    }

    static pie(data){
        if(!data.legend||!data.category||!data.value) return null;
        //检查通过，开始生成图表
        let legends=data.legend.value;
        let cdata={
            legend:[legends[0].value],
            category:data.category.value.map(e=>{return e.value}),
            series:[]
        };
        let dir=data.category.direction,legend=data.legend.value[0];
        let loc=dir==='top-bottom'?legend.c:legend.r;
        let values=getValues(data.value.value,dir,loc);
        cdata.series=[{
            name:cdata.legend,
            type:'pie',
            data:cdata.category.map((e,i)=>{
                return {name:e,value:values[i]}
            })
        }];
        let option = {
            tooltip: {
                trigger: "item",
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
            legend: {
                orient: 'vertical',
                data:cdata.legend,
                x: 'left'
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            series : cdata.series
        };
        return option
    }
    static radar(data){
        if(!data.legend||!data.category||!data.value||!data.maxvalue) return null;
        //检查通过，开始生成图表
        let legends=data.legend.value;
        let cdata={
            legend:[legends[0].value],
            category:data.category.value.map(e=>{return e.value}),
            series:[],
            indicator:[]
        };
        let dir=data.category.direction,legend=data.legend.value[0];
        let loc=dir==='top-bottom'?legend.c:legend.r;
        let values=data.value.value.map(e=>{return e.value});
        let maxvalues=data.maxvalue.value.map(e=>{return e.value});
        cdata.indicator=cdata.category.map((e,i)=>{
            return {name:e, max:maxvalues[i]}
        });
        cdata.series=[{
            name:cdata.legend,
            type:'radar',
            data:[{
                name:cdata.legend,
                value:cdata.category.map((e,i)=>{
                    return values[i]
                })
            }]
        }];
        let option = {
            tooltip: {},
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            radar: {
                indicator: cdata.indicator
            },
            series : cdata.series
        };
        return option
    }
    static scatter(data){
        if(!data.legend||!data.category||!data.value||!data.yvalue) return null;
        //检查通过，开始生成图表
        let legends=data.legend.value;
        let cdata={
            legend:[legends[0].value],
            category:data.category.value.map(e=>{return e.value}),
            series:[]
        };
        let dir=data.category.direction,legend=data.legend.value[0];
        let loc=dir==='top-bottom'?legend.c:legend.r;
        let xvalues=data.value.value.map(e=>{return e.value});
        let yvalues=data.yvalue.value.map(e=>{return e.value});
        cdata.series=[{
            name:cdata.legend,
            type:'scatter',
            large: true,
            symbolSize: 3,
            data:cdata.category.map((e,i)=>{
                return {name:e,value:[xvalues[i],yvalues[i]]}
            })
        }];
        let option = {
            tooltip: {
                trigger: "item",
                formatter: function(val){
                    return val.data.name+" : "+val.data.value.join(',');
                }
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            xAxis : [
                {
                    type : 'value',
                    scale:true
                }
            ],
            yAxis : [
                {
                    type : 'value',
                    scale:true
                }
            ],
            series : cdata.series
        };
        return option;
    }
}
export default Parser;
